package com.tangkun.environment.view.sticky;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import com.tangkun.environment.R;
import com.tangkun.environment.utils.Utils;

public class SideBar extends View {
    private OnTouchingLetterChangedListener onTouchingLetterChangedListener;
    public String[] b = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P",
            "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
    private int choose = -1;
    private final int maxLength = 27;
    private int singleHeight;
    private float defaultHeight;
    private Paint paint = new Paint();
    private Context context;
    private TextView mTextDialog;

    public void setLetter(Object[] obj) {
        this.b = (String[]) obj;
    }

    public void setTextView(TextView mTextDialog) {
        this.mTextDialog = mTextDialog;
    }

    public SideBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.context = context;
    }

    public SideBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    public SideBar(Context context) {
        super(context);
        this.context = context;
    }

    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int height = getHeight();
        int width = getWidth();

//		if(b.length>20){
        singleHeight = Utils.dp2px(context, 15);
        defaultHeight = (height - singleHeight * (b.length)) / 2.0f;
        for (int i = 0; i < b.length; i++) {
            paint.setColor(getResources().getColor(R.color.gray));
            paint.setTypeface(Typeface.DEFAULT_BOLD);
            paint.setAntiAlias(true);
            paint.setTextSize(Utils.dp2px(context, 10));
            if (i == choose) {
                paint.setColor(getResources().getColor(R.color.black));
                paint.setFakeBoldText(true);
            }
            float xPos = width / 2 - paint.measureText(b[i]) / 2;
//				float yPos = singleHeight * i + singleHeight;
            float yPos = singleHeight * i + singleHeight + defaultHeight;
            canvas.drawText(b[i], xPos, yPos, paint);
            paint.reset();
        }
//		}else{
//			int singleHeight = height / maxLength;
//			float defaultHeight=(height-singleHeight*(b.length))/2.0f;
//			for (int i = 0; i < b.length; i++) {
//				paint.setColor(getResources().getColor(R.color.gray));
//				paint.setTypeface(Typeface.DEFAULT_BOLD);
//				paint.setAntiAlias(true);
//				paint.setTextSize(20);
//				if (i == choose) {
//					paint.setColor(getResources().getColor(R.color.black));
//					paint.setFakeBoldText(true);
//				}
//				float xPos = width / 2 - paint.measureText(b[i]) / 2;
//				float yPos = singleHeight * i + singleHeight+defaultHeight;
//				canvas.drawText(b[i], xPos, yPos, paint);
//				paint.reset();
//			}
//		}	


    }

    @SuppressLint("NewApi")
    @SuppressWarnings("deprecation")
    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        final int action = event.getAction();
        final float y = event.getY();
        final int oldChoose = choose;
        final OnTouchingLetterChangedListener listener = onTouchingLetterChangedListener;
        final int c = (int) ((y - defaultHeight) / (singleHeight * b.length) * b.length);
        switch (action) {
            case MotionEvent.ACTION_UP:
                setBackgroundDrawable(new ColorDrawable(0x00000000));
                choose = -1;//
                invalidate();
                if (mTextDialog != null) {
                    mTextDialog.setVisibility(View.INVISIBLE);
                }
                break;

            default:
                setBackgroundResource(R.drawable.sidebar_background);
                setAlpha((float) 0.7);
                if (oldChoose != c) {
                    if (c >= 0 && c < b.length) {
                        if (listener != null) {
                            listener.onTouchingLetterChanged(b[c]);
                        }
                        if (mTextDialog != null) {
                            mTextDialog.setText(b[c]);
                            mTextDialog.setVisibility(View.VISIBLE);
                        }

                        choose = c;
                        invalidate();
                    }
                }

                break;
        }
        return true;
    }

    public void setOnTouchingLetterChangedListener(OnTouchingLetterChangedListener onTouchingLetterChangedListener) {
        this.onTouchingLetterChangedListener = onTouchingLetterChangedListener;
    }

    public interface OnTouchingLetterChangedListener {
        public void onTouchingLetterChanged(String s);
    }

}